﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_PartReceive : UserControl
    {
        public panel_PartReceive()
        {
            InitializeComponent();
            LoadData();
            //this.PartReceive.GotFocus += PartReceive_GotFocus;
        }
        /*
        void PartReceive_GotFocus(object sender, EventArgs e)
        {
            LoadData();
        } */

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT [241_PartReceive].ID, [241_PartReceive].POID, [241_PartReceive].OrderNo, [241_PODetails].PartNo,"
                + " [222_Part].IQID, [241_PartReceive].Quantity, [241_PartReceive].ArrivalDate, [241_PartReceive].Note,"
                + " [241_PartReceive].Finished, [241_PartReceive].InvoiceNo, [241_PartReceive].CusDeclNo,"
                + " [241_CustomClearance].RegisterDate, [222_Part].UnitID, [222_Part].IQUnit, [222_Part].UnitExchange,"
                + " [241_PODetails].Quantity FROM [241_PartReceive]"
                + " LEFT JOIN [241_PODetails] ON [241_PartReceive].POID=[241_PODetails].POID"
                + " AND [241_PartReceive].OrderNo=[241_PODetails].OrderNo"
                + " LEFT JOIN [222_Part] ON [241_PODetails].PartNo = [222_Part].PartNo"
                + " LEFT JOIN [241_CustomClearance] ON [241_PartReceive].CusDeclNo = [241_CustomClearance].ID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridPartReceive.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("OrderID", typeof(string));
                newTable.Columns.Add("PartID", typeof(string));
                newTable.Columns.Add("IQID", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("ArrivalDate", typeof(DateTime));
                newTable.Columns.Add("Note", typeof(string));
                newTable.Columns.Add("Finished", typeof(int));
                newTable.Columns.Add("InvoiceNo", typeof(string));
                newTable.Columns.Add("CusDeclNo", typeof(string));
                newTable.Columns.Add("CusDeclDate", typeof(DateTime));
                newTable.Columns.Add("UnitID", typeof(string));
                newTable.Columns.Add("IQUnit", typeof(string));
                newTable.Columns.Add("UnitExchange", typeof(float));
                newTable.Columns.Add("CusQuantity", typeof(float));
                gridPartReceive.DataSource = newTable;
            }
            PartReceive.Columns[1].Caption = "POID";
            PartReceive.Columns[2].Caption = "Order No";
            PartReceive.Columns[3].Caption = "Part No";
            PartReceive.Columns[4].Caption = "IQID";
            PartReceive.Columns[5].Caption = "Quantity";
            PartReceive.Columns[6].Caption = "Arrival Date";
            PartReceive.Columns[7].Caption = "Note";
            PartReceive.Columns[8].Caption = "Finished";
            PartReceive.Columns[9].Caption = "Invoice No";
            PartReceive.Columns[10].Caption = "Custom Declare No";
            PartReceive.Columns[11].Caption = "Custom Declare Date";
            PartReceive.Columns[12].Caption = "UnitID";
            PartReceive.Columns[13].Caption = "IQUnit";
            PartReceive.Columns[14].Caption = "Unit Exchange";
            PartReceive.Columns[15].Caption = "Custom Quantity";
            PartReceive.Columns[0].Visible = false;
            PartReceive.BestFitColumns();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_PartReceive add_Form = new frm_Add_PartReceive();
            add_Form.Show();
        }
    }
}
